import Reflux from 'reflux';
import PageConst from './PageConst';
import Actions from './Actions';
import update from 'react-addons-update';
import { Toast } from 'antd-mobile';
import Utils from '../../../utils/index'
import DB from '../../../app/db';
import StorageUtils from '../../../app/storage'
import { Control } from 'react-keeper'
import {getInfoByOpenId} from "./const/Const";

export default Reflux.createStore({
  listenables: [Actions],
  data: {
    ...PageConst,
    showWarn: false,
    warnTimer: null,
    codeTimer: null,
    warnMessage: '',
    count: 60,
    showCodeTimer: true,
    vcode: '',
    showCertOK:false,
    showPhoneOK:false,
    phone: '',
    userInfo: {
      certNum: '',
      telNum: '',
      vcode: ''
    }
  },

  initMode: function () {
    this.data.userInfo = update(this.data.userInfo, {$merge: {...StorageUtils.getUser()}});
    this.updateComponent();
    let userInfo = StorageUtils.getUser();
    if (userInfo && userInfo.openId) {
      getInfoByOpenId(userInfo.openId);
    }
  },

  onCheckCertNum: function (e) {

  let t = this;
  let resultCertNum = Utils.checkCard(this.data.userInfo.certNum);
  if (!resultCertNum.success) {
    console.log(resultCertNum);
      this.showWarnMessage(e,resultCertNum.message);
      return;
  }

  if (resultCertNum.success) {
        DB.WechatAPI.checkUserCertNum({
          cert: this.data.userInfo.certNum,
        }).then(
          function (content) {
              let resultObj = Utils.isEmptyObject(content);

              console.info(resultObj.success);
              if (!resultObj.success) {
                t.showWarnMessage(e,'用户未注册');
              }
              else
              {
                t.data.userInfo = update(t.data.userInfo, {telNum: {$set: content.phone}});
                let resultPhone = Utils.checkPhone(t.data.userInfo.telNum);
                if (resultPhone.success) {
                  t.data.showPhoneOK = update(t.data.showPhoneOK, {$set: true});
                  t.data.showCertOK = update(t.data.showCertOK, {$set: true});
                  t.data.userInfo = update(t.data.userInfo, {telNum: {$set: content.phone}});
                  t.data.phone = update(t.data.phone,  {$set: t.data.userInfo.telNum});


                  DB.WechatAPI.binding(t.data.userInfo).then(
                    function (content) {
                      Control.go('/bindinghome/ok');
                    },
                    function (error) {
                      console.log(error);
                    });

                  t.updateComponent();
                }
                else {
                  Control.go('/home');
                }

                console.log(content);

              t.updateComponent();
            };

          },
          function (error) {
            console.info('11_erro');
            console.log(error);
          });
  }
},

  showWarnMessage: function (e,message) {
    Toast.info(message, 1);
  },


  onEnterInfo: function (e) {
    let resultCertNum = Utils.checkCard(this.data.userInfo.certNum);
    if (!resultCertNum.success) {
      this.data.userInfo = update(this.data.userInfo, {certNum: {$set: ''}});
      this.showWarnMessage(e,resultCertNum.message);
      return;
    }

    this.data.userInfo = update(this.data.userInfo, {$merge: {...StorageUtils.getStorage().get('user')}});

    DB.WechatAPI.binding(this.data.userInfo).then(
      function (content) {
        Control.go('/bindinghome/ok');
      },
      function (error) {
        console.log(error);
      });
  },



  onChangeVcode: function (vcode) {

    this.data.userInfo = update(this.data.userInfo, {vcode: {$set: vcode}});


    this.updateComponent();
  },

  onChangeCertNum: function (certNum) {
    console.log(certNum);
    this.data.userInfo = update(this.data.userInfo, {certNum: {$set: certNum}});
    this.updateComponent();
  },

  onChangeTeleNumber: function (telNum) {
    this.data.userInfo = update(this.data.userInfo, {telNum: {$set: telNum}});
    this.updateComponent();
  },

  updateComponent: function () {
    this.trigger(this.data);
  },

  getInitialState: function () {
    return this.data;
  }
});
